/*
    This sketch runs fine on:

    (1) Arduino + RF shield
    (2) Arduino + Ethernet shield + RF shield    
*/

#include <Ports.h>
#include <RF12.h>

#define DEBUG        

// internal data
struct {
  double realPower;
  double apparentPower;
  double powerFactor;
  double Vrms;
  double Irms;
byte lobat :
  1;  // supply voltage dropped under 3.1V: 0..1
} payload;

void setup () {

#ifdef DEBUG
  Serial.begin(57600);
  Serial.println(57600);
  Serial.println("Receive EnerMon output");
#endif
    
    rf12_config();
    delay(1000);
}

void rf12_task() {
  
  if (rf12_recvDone() && rf12_crc == 0) {
  //if (rf12_recvDone()) {
    memcpy(&payload, (void*) rf12_data, sizeof payload);

#ifdef DEBUG
    Serial.print("OK ");
    Serial.print(payload.realPower);
    Serial.print(' ');
    Serial.print(payload.apparentPower);
    Serial.print(' ');
    Serial.print(payload.powerFactor);
    Serial.print(' ');
    Serial.print(payload.Vrms);
    Serial.print(' ');
    Serial.print(payload.Irms);
    Serial.print(' ');
    Serial.print(payload.lobat, DEC);
    Serial.println(' ');      
    Serial.print("source node:");
    Serial.println(RF12_HDR_MASK & rf12_hdr, DEC);
#endif

  }
}

void loop () {
  
  Serial.println("MAIN LOOP");
  Serial.println("Eseguo rf12_task()");
    rf12_task();
  Serial.println("Ritorno da rf12_task()");

  delay(1000);
}
